// ====================CardGamePhase====================

class CardGamePhase {
    constructor() {
        this.initialize();
    }

    initialize() {
    }

    onEnter() {
    }

    onUpdate() {
    }

    onExit() {
    }
}

class CardPhase_GameStart extends CardGamePhase {

    initialize() {
        const drawCardAmount = SDS_CARD_PLUGIN_PARAM.gameOption.startHandCardAmount;
        this.context = {drawCardAmount};
    }

    onEnter() {
        // TODO 时点-对局开始
        // console.log("阶段-对局开始", this.context.drawCardAmount);
        CardGameManager.deal(this.context.drawCardAmount);
    }

    onUpdate() {
        // TODO 如果不允许换牌，则直接进入下一阶段，否则等待某个变量为真
        if (!CardGameManager._cardPanel.isAnimation()) {
            CardGameManager.changePhase("turn");
        }

    }

    onExit() {
    }
}

class CardPhase_Turn extends CardGamePhase {
    onEnter() {
        // console.log("阶段-回合");
        this.waitCount = 0;
        this.effectIndex = 0;
        CardGameManager.cardTurnStart();
        CardGameManager.tip("玩家回合");
    }

    onUpdate() {
        if (this.waitCount > 0) {
            this.waitCount--;
            return;
        }
        if (!CardGameManager.isEffecting() && !!CardGameManager.usingCard) {
            this.updateEffect();
        }
        this.updateTurnEnd();
    }

    /**
     * 更新效果
     */
    updateEffect() {
        const effect = CardGameManager.usingCard.actionSeq()[this.effectIndex];
        if (!effect) {
            this.effectIndex = 0;
            CardGameManager.cardEnd();
        } else {
            const {name, args} = effect;
            switch (name) {
                case "行动":
                    CardGameManager.makeCardAction();
                    break;
                case "过牌":
                    CardEffect.drawCards(args);
                    break;
                case "能量":
                    CardEffect.gainEnergy(args);
                    break;
                case "护甲":
                    CardEffect.gainArmor(args);
                    break;
                case "检索":
                    CardEffect.retrieve(args);
                    break;
                case "发现":
                    CardEffect.discover(args);
                    break;
                case "弃牌":
                    CardEffect.abandonCard(args);
                    break;
                case "抉择":
                    CardEffect.handCardChoice(args);
                case "等待":
                    const waitTime = Number(args[0]);
                    if (!!waitTime) {
                        this.waitCount = waitTime;
                    }
                    break;
                default:
                    console.warn(`未知的卡牌效果，清检查是否填写错误,位于id:${CardGameManager.usingCard.id()} 效果${name} 参数${args}`);
                    break;
            }
            this.effectIndex++;
        }
    }

    updateTurnEnd() {
        if (CardGameManager.isTurnEnd() && !CardGameManager.isBattlerActioning()) {
            CardGameManager.changePhase("turnEnd");
        }
    }
}

class CardPhase_TurnEnd extends CardGamePhase {
    onEnter() {
        CardGameManager.tip("敌人回合");
        CardGameManager.makeEnemyAction();
    }

    onUpdate() {
        if (!CardGameManager.isBattlerActioning()) {
            CardGameManager.changePhase("turn");
        }
    }
}

class CardPhase_GameEnd extends CardGamePhase {
    onEnter() {
        this.cardGameSettlement();
    }

    cardGameSettlement() {
        const actor = CardGameManager.cardBattler();
        const data = [];
        const gold = $gameTroop.goldTotal();
        const cards = actor.allClassCard().random(3);
        if (!!gold) {
            data.push({type: "gold", amount: gold});
        }
        if (cards.length > 0) {
            data.push({type: "card", cards});
        }
        CardGameManager.settlement(data);
    }
}